#如果希望不管是否发生异常,都执行某段代码(比如关闭连接,释放资源等),则使用finally子句
# num1 = 10
# num2 = 0
#
# try:
#     res = num1 / num2
#     print("hi...")
# except Exception as e:
#     print(f"出现了异常,异常信息->{e}")
# else:
#     print("执行了else...")
# finally:
#     print("执行了 finally~~~")
#
# print("继续执行~~~")



"""
可以有多个except字句,捕获不同的异常(进行不同的业务处理),如果发生异常,只会匹配一个except
建议把具体的异常写在前面,积累异常在后面比如(IndexError在前,Exception在后),这样当具体异常匹配不到的时候
再由积累异常匹配
"""
# num1 = 10
# num2 = 0
# try:
#     res = num1 / num2 #异常Zero
#     str_a = "hello"
#     print(str_a[20])#异常IndexError
#     open("d:/temp/temp/temp.txt" , "r" , encoding="utf-8")
#     print("hi....")
# #试试看,把积累异常写到前面,会怎么样？ 会全部走到这个异常，下面的就不会走了
# except Exception as e:
#     print(f"出现了异常Exception异常信息->{e}")
# except IndexError as e:
#     print(f"出现了异常IndexError 异常信息->{e}")
# except ZeroDivisionError as e:
#     print(f"出现了异常ZeroDivisionError 异常信息->{e}")
#
#
# print("程序继续执行...")


#一个except字句,也可以捕获不同的异常
num1 = 10
num2 = 0
try:
    res = num1 / num2
    str_a = "hello"
    print(str_a[20])
    open("d:/temp/temp/temp.txt" , "r" , encoding="utf-8")
    print("hi....")
except(IndexError , ZeroDivisionError , NameError , FileNotFoundError) as e:
  print(f"捕获多种异常 信息->{e} 类型{type(e)}")
finally:
    print("执行了finally~~~~")

print("继续执行~~~")